/*
 * The contents of this file are subject to the Mozilla Public License
 * Version 1.1 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS"
 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 * License for the specific language governing rights and limitations
 * under the License.
 * 
 * The Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 *
 * The Initial Developer of the Original Code is iSQL-Viewer, A Mutli-Platform Database Tool.
 * Portions created by Mark A. Kobold are Copyright (C) 2000-2007. All Rights Reserved.
 *
 * Contributor(s): 
 *  Mark A. Kobold [mkobold <at> isqlviewer <dot> com].
 *  
 * If you didn't download this code from the following link, you should check
 * if you aren't using an obsolete version: http://www.isqlviewer.com
 */
package org.isqlviewer.ui.dnd;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.Transferable;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.IOException;

import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;

/**
 * Drop Target for dropping text with COPY/MOVE DnD actions.
 * <p>
 * The default drop targets for text only want to seem to do COPY only.
 * 
 * @author Mark A. Kobold &lt;mkobold at isqlviewer dot com&gt;
 * @version 1.0
 */
public class TextComponentDropTarget implements DropTargetListener {

    private static final long serialVersionUID = 4496993389366518143L;
    private JTextComponent editor = null;

    public TextComponentDropTarget(JTextComponent editor) {

        this.editor = editor;
    }
    public void drop(DropTargetDropEvent event) {
       
        boolean successful = false;
        try {
            if (event.isDataFlavorSupported(DataFlavor.stringFlavor)) {
                Document doc = editor.getDocument();
                int offset = editor.viewToModel(event.getLocation());
                int dropAction = event.getDropAction();
                Transferable transferable = event.getTransferable();
                String s = (String) transferable.getTransferData(DataFlavor.stringFlavor);
                event.acceptDrop(dropAction);
                switch (dropAction) {
                    case DnDConstants.ACTION_COPY :
                        doc.insertString(offset, s, null);
                        editor.requestFocus();
                        successful = true;
                        break;
                    case DnDConstants.ACTION_MOVE :
                        editor.setText(s);
                        editor.requestFocus();
                        successful = true;
                        break;
                    default :
                        break;
                }
            }
        } catch (UnsupportedFlavorException e) {
        } catch (IOException e) {
        } catch (BadLocationException e) {
        } finally {
            event.dropComplete(successful);
        }
    }
    /* (non-Javadoc)
     * @see java.awt.dnd.DropTargetListener#dragEnter(java.awt.dnd.DropTargetDragEvent)
     */
    public void dragEnter(DropTargetDragEvent dtde) {

        // TODO Auto-generated method stub
        
    }
    /* (non-Javadoc)
     * @see java.awt.dnd.DropTargetListener#dragExit(java.awt.dnd.DropTargetEvent)
     */
    public void dragExit(DropTargetEvent dte) {

        // TODO Auto-generated method stub
        
    }
    /* (non-Javadoc)
     * @see java.awt.dnd.DropTargetListener#dragOver(java.awt.dnd.DropTargetDragEvent)
     */
    public void dragOver(DropTargetDragEvent dtde) {

        // TODO Auto-generated method stub
        
    }
    /* (non-Javadoc)
     * @see java.awt.dnd.DropTargetListener#dropActionChanged(java.awt.dnd.DropTargetDragEvent)
     */
    public void dropActionChanged(DropTargetDragEvent dtde) {

        // TODO Auto-generated method stub
        
    }
}